< previous page page_205 next page >

Page 205
0205-01.jpg
Figure 7.11
A minimum rank filter gives an impressionistic quality.
Bilinear Interpolation
When you want to rotate, stretch, or otherwise transform an image, you often face the problem that you do not have enough data to give values to all the pixels in the resulting image. For example, if you increase an image's height and width by a factor of two, you increase the number of pixels in the image by a factor of four. You have only a quarter of the data you need to assign values to all the pixels.
The PaintPicture method solves this problem by repeating the values of the pixels it has. If you double the width and height of an image, it uses each pixel value four times to fill in the values it needs. If you enlarge the image enough, the image appears blocky.
Example program Stretch, shown in Figure 7.12, uses PaintPicture to display an image at 1, 4, 8, and 16 times its original size. At 8 and 16 times its original size, the image is quite blocky.
Instead of repeating pixel values, you can use the values you know to guess the values in between. The basic idea is that an unknown pixel's color value should be spaced proportionally between the values of the pixels around it. If an unknown pixel is halfway between two known pixels, its value should be halfway between their values. If the unknown pixel is closer to one than the other, its value should be nearer to the value of the closer pixel.
More precisely, if the known pixels have values V1 and V2, and the distances from the unknown pixel to the known pixels are D1 and D2, the unknown pixel should have the value:
e10a6a0a59f035e870d90cca05b85fce.gif
V = (V1 * D2 + V2 * D1) / (D1 + D2)
This equation has several important properties. If the unknown pixel is very close to the first known pixel, D1 is close to zero. Then the V2 * D1 term is close to zero, and the (D1 + D2) term is close to D2. The result becomes V = V1 * D2 / D2 = V1. This makes sense:

 
< previous page page_205 next page >